<?php

namespace app\admin\controller;

//订单管理
class Rush extends Common
{
    //订单列表
    public function rush_list()
    {
        $where = array('a.r_id' => array('gt', 0));
        $status = Request()->param('status');
        //状态
        $day = Request()->param('day');
        $page = Request()->param('page');
        $size = Request()->param('size');
        if ($page < 1) {
            $page = 1;
        }
        if ($size < 15) {
            $size = 15;
        }
        $now = ($page - 1) * $size;
        if ($status > 0) {
            if($status == 1){
                $where['a.r_status'] = 1;
            }else{
                $where['a.r_status'] = 2;
            }
        }
        if ($day != '') {
            $where['a.r_date'] = array('like', '%' . $day . '%');
        }
        $rush = db('rush a')
            ->field("a.*,count(b.rg_id) as g_num")
            ->join('fr_rush_goods b', 'a.r_id = b.r_id', 'left')
            ->order('a.r_date desc,r_status')
            ->group("a.r_id")
            ->where($where)
            ->limit("$now,$size")
            ->select();
        //显示抢购列表
        $count = db('rush a')
            ->where($where)
            ->count();
        //等级信息
        $page_str = page($page, $size, $count, count($rush));//分页
        $this->view->assign('page_str', $page_str);
        $this->view->assign('data', $rush);
        $this->assign('status', $status);
        $this->assign('day', $day);
        $this->assign('page', $page);
        $this->assign('size', $size);
        $this->assign('count', $count);
        return $this->view->fetch();

    }
    //添加计划
    function add_rush(){
        if(request()->isGet()){
            return $this->fetch();
        }else if(request()->isAjax()){
            $data=request()->post();
            $date=$data['r_date'];
            if($date==''){
                return return_json(array('code'=>0,'msg'=>'抢购日期不能为空'),'添加抢购计划');
            }
            if($date<date('Y-m-d')){
                return return_json(array('code'=>0,'msg'=>'计划日期不能是今天之前'),'添加抢购计划');
            }
            if($data['start_time']==''){
                $data['start_time']='00:00:00';
            }
            if($data['end_time']==''){
                $data['end_time']='23:59:59';
            }
            if($data['end_time']<=$data['start_time']){
                return return_json(array('code'=>0,'msg'=>'结束时间必须在开始时间之后'),'添加抢购计划');
            }
            $rush=db('rush')
                ->where(array('r_date'=>$date,'r_status'=>1))
                ->find();
            if(!empty($rush)){
                return return_json(array('code'=>0,'msg'=>'已有相同日期的抢购计划'),'添加抢购计划');
            }
            $data1=array('r_date'=>$date,'r_start_time'=>strtotime($date.$data['start_time']),'r_end_time'=>strtotime($date.$data['end_time']),'r_status'=>$data['r_status'],'r_create_time'=>time(),'r_add_user_id'=>session('uid'),'r_add_user_name'=>session('user.real_name'));
            $res=db('rush')->insertGetId($data1);
            if($res>0){
                return return_json(array('code'=>1,'msg'=>'计划添加成功'),'添加抢购计划');
            }else{
                return return_json(array('code'=>1,'msg'=>'计划添加失败'),'添加抢购计划');
            }
        }
    }
    //添加计划商品
    function add_goods(){
        if(request()->isGet()){
            $id=request()->get('id');
            $goods=db('goods')
                ->where(array('g_id'=>$id))
                ->find();
            $rush=db('rush')
                ->where(array('r_status'=>1,'r_date'=>array('egt',date('Y-m-d'))))
                ->order("r_date")
                ->select();
            $this->assign('id',$id);
            $this->assign('goods',$goods);
            $this->assign('rush',$rush);
            return $this->fetch();
        }else if(request()->isAjax()) {
            $data = request()->post();
            $id=$data['id'];
            $goods=db('goods')
                ->where(array('g_id'=>$id))
                ->find();
            if($goods['g_status']!=1){
                return return_json(array('code'=>0,'msg'=>'商品已冻结无法进行抢购'),'添加计划商品');
            }
            $rush=db('rush')
                ->where(array('r_status'=>1,'r_date'=>array('egt',date('Y-m-d')),'r_id'=>$data['r_id']))
                ->order("r_date")
                ->find();
            if(empty($rush)){
                return return_json(array('code'=>0,'msg'=>'抢购计划出错'),'添加计划商品');
            }
            $rush_goods=db('rush_goods')
                ->where(array('r_id'=>$data['r_id'],'g_id'=>$id,'is_full'=>0))
                ->find();
            if(!empty($rush_goods)){
                return return_json(array('code'=>0,'msg'=>'改商品已在该抢购计划当中'),'添加计划商品');
            }
            if($data['rg_all']<1){
                return return_json(array('code'=>0,'msg'=>'抢购总量必须大于1'),'添加计划商品');
            }
            if($data['rg_sort']<=0){
                $data['rg_sort']=0;
            }
            $data1=array('r_id'=>$data['r_id'],'g_id'=>$id,'rg_sort'=>$data['rg_sort'],'rg_status'=>$data['rg_status'],'rg_all'=>$data['rg_all'],'rg_create_time'=>time(),'rg_add_user_id'=>session('uid'),'rg_add_user_name'=>session('user.real_name'));
            $res=db('rush_goods')->insertGetId($data1);
            if($res>0){
                return return_json(array('code'=>1,'msg'=>'计划添加成功'),'添加抢购计划');
            }else{
                return return_json(array('code'=>1,'msg'=>'计划添加失败'),'添加抢购计划');
            }
        }
    }
    //禁用抢购计划
    function rush_del(){
        if(request()->isAjax()){
            $id=request()->post('id');
            $rush=db('rush')
                ->where(array('r_id'=>$id))
                ->find();
            if($rush['r_date']<=date('Y-m-d')){
                return return_json(array('code'=>0,'msg'=>'已到开始时间，无法冻结'),'禁用抢购计划');
            }
            $res=db('rush')
                ->where(array('r_id'=>$id))
                ->update(array('r_status'=>2));
            if($res!==false){
                return return_json(array('code'=>1,'msg'=>'禁用成功'),'禁用抢购计划');
            }else{
                return return_json(array('code'=>0,'msg'=>'禁用失败'),'禁用抢购计划');
            }
        }
    }
    //启用抢购计划
    function rush_use(){
        if(request()->isAjax()){
            $id=request()->post('id');
            $rush=db('rush')
                ->where(array('r_id'=>$id))
                ->find();
            if($rush['r_date']<=date('Y-m-d')){
                return return_json(array('code'=>0,'msg'=>'已到开始时间，无法启用'),'启用抢购计划');
            }
            $rush1=db('rush')
                ->where(array('r_date'=>$rush['r_date'],'r_status'=>1))
                ->find();
            if(!empty($rush1)){
                return return_json(array('code'=>0,'msg'=>'已有相同日期的抢购计划'),'启用抢购计划');
            }
            $res=db('rush')
                ->where(array('r_id'=>$id))
                ->update(array('r_status'=>1));
            if($res!==false){
                return return_json(array('code'=>1,'msg'=>'启用成功'),'启用抢购计划');
            }else{
                return return_json(array('code'=>0,'msg'=>'启用失败'),'启用抢购计划');
            }
        }
    }
    //修改抢购计划
    function rerush(){
        if(request()->isGet()){
            $id=request()->get('id');
            $rush=db('rush')
                ->where(array('r_id'=>$id))
                ->find();
            $this->assign('id',$id);
            $this->assign('rush',$rush);
            return $this->fetch();
        }else if(request()->isAjax()) {
            $data = request()->post();
            $id=$data['id'];
            $rush=db('rush')
                ->where(array('r_id'=>$id))
                ->find();
            if($rush['r_date']<=date('Y-m-d')){
                return return_json(array('code'=>0,'msg'=>'已到开始时间，无法修改'),'修改抢购计划');
            }
            $date=$data['r_date'];
            if($date==''){
                return return_json(array('code'=>0,'msg'=>'抢购日期不能为空'),'修改抢购计划');
            }
            if($date<date('Y-m-d')){
                return return_json(array('code'=>0,'msg'=>'计划日期不能是今天之前'),'修改抢购计划');
            }
            if($data['start_time']==''){
                $data['start_time']='00:00:00';
            }
            if($data['end_time']==''){
                $data['end_time']='23:59:59';
            }
            if($data['end_time']<=$data['start_time']){
                return return_json(array('code'=>0,'msg'=>'结束时间必须在开始时间之后'),'修改抢购计划');
            }
            $rush=db('rush')
                ->where(array('r_date'=>$date,'r_status'=>1))
                ->find();
            if(!empty($rush)){
                return return_json(array('code'=>0,'msg'=>'已有相同日期的抢购计划'),'修改抢购计划');
            }
            $data1=array('r_date'=>$date,'r_start_time'=>strtotime($date.$data['start_time']),'r_end_time'=>strtotime($date.$data['end_time']),'r_status'=>$data['r_status']);
            $res=db('rush')
                ->where(array('r_id'=>$id))
                ->update($data1);
            if($res!==false){
                return return_json(array('code'=>1,'msg'=>'计划修改成功'),'修改抢购计划');
            }else{
                return return_json(array('code'=>1,'msg'=>'计划修改失败'),'修改抢购计划');
            }
        }
    }
    //计划商品
    function goods_list(){
        $id=request()->get('id');
        $where['a.r_id']=array('gt',0);
        $page = Request()->param('page');
        $size = Request()->param('size');
        $name = Request()->param('name');
        $status = Request()->param('status');
        if ($page < 1) {
            $page = 1;
        }
        if ($size < 15) {
            $size = 15;
        }
        $now = ($page - 1) * $size;
        if($status>0){
            if($status==1){
                $where['a.rg_status']=1;
            }else{
                $where['a.rg_status']=array('neq',1);
            }
        }
        if($name!=''){
            $where['b.g_name']=array('like','%'.$name.'%');
        };
        $rush=db('rush')
            ->where(array('r_id'=>$id))
            ->find();
        $where['r_id']=$id;
        $goods=db('rush_goods a')
            ->field("a.*,b.g_name")
            ->join("fr_goods b","a.g_id = b.g_id","left")
            ->where($where)
            ->order("a.rg_sort desc")
            ->limit("$now,$size")
            ->select();
        $count = db('rush_goods a')
            ->join("fr_goods b","a.g_id = b.g_id","left")
            ->where($where)
            ->count();
        //等级信息
        $page_str = page($page, $size, $count, count($goods));//分页
        $this->view->assign('page_str', $page_str);
        $this->view->assign('rush', $rush);
        $this->view->assign('data', $goods);
        $this->view->assign('page', $page);
        $this->view->assign('size', $size);
        $this->view->assign('status', $status);
        $this->view->assign('name', $name);
        $this->view->assign('count', $count);
        $this->view->assign('id', $id);
        return $this->fetch();
    }
    //禁用抢购计划商品
    function goods_del(){
        if(request()->isAjax()){
            $id=request()->post('id');
            $res=db('rush_goods')
                ->where(array('rg_id'=>$id))
                ->update(array('rg_status'=>2));
            if($res!==false){
                return return_json(array('code'=>1,'msg'=>'禁用成功'),'禁用抢购计划商品');
            }else{
                return return_json(array('code'=>0,'msg'=>'禁用失败'),'禁用抢购计划商品');
            }
        }
    }
    //启用抢购计划商品
    function goods_use(){
        if(request()->isAjax()){
            $id=request()->post('id');
            $res=db('rush_goods')
                ->where(array('rg_id'=>$id))
                ->update(array('rg_status'=>1));
            if($res!==false){
                return return_json(array('code'=>1,'msg'=>'启用成功'),'启用抢购计划商品');
            }else{
                return return_json(array('code'=>0,'msg'=>'启用失败'),'启用抢购计划商品');
            }
        }
    }
    //修改计划商品
    function regoods(){
        if(request()->isGet()){
            $id=request()->get('id');
            $goods=db('rush_goods a')
                ->field("a.*,b.g_name")
                ->join("fr_goods b","a.g_id = b.g_id","left")
                ->where(array('a.rg_id'=>$id))
                ->find();
            $rush=db('rush')
                ->where(array('r_status'=>1,'r_date'=>array('egt',date('Y-m-d'))))
                ->order("r_date")
                ->select();
            $this->assign('id',$id);
            $this->assign('goods',$goods);
            $this->assign('rush',$rush);
            return $this->fetch();
        }else if(request()->isAjax()) {
            $data = request()->post();
            $id=$data['id'];
            $goods=db('rush_goods a')
                ->field("a.*,b.g_name")
                ->join("fr_goods b","a.g_id = b.g_id","left")
                ->where(array('a.rg_id'=>$id))
                ->find();
            if(empty($goods)){
                return return_json(array('code'=>0,'msg'=>'商品出错'),'修改计划商品');
            }
            $rush=db('rush')
                ->where(array('r_status'=>1,'r_date'=>array('egt',date('Y-m-d')),'r_id'=>$data['r_id']))
                ->order("r_date")
                ->find();
            if(empty($rush)){
                return return_json(array('code'=>0,'msg'=>'抢购计划出错'),'修改计划商品');
            }
            if($data['rg_all']<1){
                return return_json(array('code'=>0,'msg'=>'抢购总量必须大于1'),'修改计划商品');
            }
            if($data['rg_sort']<=0){
                $data['rg_sort']=0;
            }
            $data1=array('r_id'=>$data['r_id'],'rg_sort'=>$data['rg_sort'],'rg_status'=>$data['rg_status'],'rg_all'=>$data['rg_all']);
            $res=db('rush_goods')->where(array('rg_id'=>$id))->update($data1);
            if($res!==false){
                return return_json(array('code'=>1,'msg'=>'计划添加成功'),'修改计划商品');
            }else{
                return return_json(array('code'=>1,'msg'=>'计划添加失败'),'修改计划商品');
            }
        }
    }
}
